<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        //判断闰年isleadyear
        function isleafyear(year) {
            return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
        }
        //判断月份天数monthdays
        function monthdays(year, month) {
            var days = 0;
            switch (month) {
                case 4:
                case 6:
                case 9:
                case 11:
                    days = 30;
                    break;
                case 2:
                    days = isleafyear(year) ? 29 : 28;
                    break;
                default:
                    days = 31;
                    break;
            }
            return days;
        }
        //判断指定月份到1900-1-1的天数. calcTotalDays
        function calcTotalDays(year, month) {
            var totaldays = 1;
            for (i = 1; i < month; i++) {
                totaldays += monthdays(year, i);
            }
            for (i = 1900; i < year; i++) {
                totaldays += isleafyear(year) ? 366 : 365;
            }
            return totaldays;
        }
        //判断指定年月是星期几clacweekdays
        function clacweekdays(year, month) {
            var totaldays = calcTotalDays(year, month);
            var weekdays;
            weekdays = totaldays % 7;
            return weekdays;
        }
        //打印日历printMonthCalendar
        function printMonthCalendar(year, month) {
            var week = "日\t一\t二\t三\t四\t五\t六\n"
            var branks = clacweekdays(year, month)
            for (i = 0; i < branks; i++) {
                week += "\t"
            }
            var days = monthdays(year, month)
            for (i = 1; i <= days; i++) {
                week += i + "\t"
                if ((i + branks) % 7 === 0) {
                    week += "\n";
                }
            }
            return week;
        }
        document.write(isleafyear(2020) + "\n");
        document.write(monthdays(2015, 3) + "\n");
        document.write(calcTotalDays(2016, 6) + "\n");
        document.write(clacweekdays(2016, 6) + "\n");

        console.log(printMonthCalendar(2020, 5));
    </script>
</body>

</html>